package cn.echoedu.command;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import cn.echoedu.command.ContactsListCommand.ContactsListFailureResponse;
import cn.echoedu.command.base.ExecuteParam;
import cn.echoedu.command.base.FailureParam;
import cn.echoedu.command.base.SuccessParam;
import cn.echoedu.entity.TelContacts;
import cn.echoedu.util.L;

import com.google.gson.Gson;

public class ContactsMakeFriendCommand extends BaseCommand{
    private final String T="ContactsMakeFriendCommand-------->";

	public void executeSuccess(SuccessParam successparam) {
		// TODO Auto-generated method stub
		List<TelContacts> telcontacts=(List<TelContacts>)successparam.getParam();
		List<ContactsMakeFriendEntity> listcontact=new ArrayList<ContactsMakeFriendEntity>();
		ContactsMakeFriendSuccessResponse contactsmakefriendsuccess=new ContactsMakeFriendSuccessResponse();
		for(TelContacts telcontact:telcontacts){
			ContactsMakeFriendEntity contactentity=new ContactsMakeFriendEntity();
			contactentity.setContact_name(telcontact.getTelcontactsname());
			 L.p(T+"name:"+telcontact.getTelcontactsname());
			contactentity.setContact_number(telcontact.getTel());
			contactentity.setContact_photo_url(telcontact.getImage().getImagesrc());
			listcontact.add(contactentity);
		}
		contactsmakefriendsuccess.setList(listcontact);
		Gson gson=new Gson();
		String sendjson=gson.toJson(contactsmakefriendsuccess);
		 send(sendjson,successparam.getResponse());
    }
    public void executeFailure(FailureParam failureparam) {
		// TODO Auto-generated method stub
    	ContactsMakeFriendFailureResponse contactlistfailure=new ContactsMakeFriendFailureResponse();
        Gson gson=new Gson();
        String jsonstr=gson.toJson(contactlistfailure);
        String sendjson="";
        try {
    		sendjson=java.net.URLEncoder.encode(jsonstr,"utf-8");
    	} catch (UnsupportedEncodingException e1) {
    		// TODO Auto-generated catch block
    		e1.printStackTrace();
    	}
//    	   java.net.URLDecoder.decode(s)
    		PrintWriter pw;
    		try {
    			failureparam.getResponse().setCharacterEncoding("utf-8");
    			pw = failureparam.getResponse().getWriter();
    		    pw.print(sendjson);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
	}
    public void execute(ExecuteParam executeparam) {
		// TODO Auto-generated method stub
		
	}
    public class ContactsMakeFriendSuccessResponse {
    	private int code=302;
    	private String msg="MakeFriend Success";
    	private List<ContactsMakeFriendEntity> list;
    	public int getCode() {
    		return code;
    	}
    	public void setCode(int code) {
    		this.code = code;
    	}
    	public String getMsg() {
    		return msg;
    	}
    	public void setMsg(String msg) {
    		this.msg = msg;
    	}
    	public List<ContactsMakeFriendEntity> getList() {
    		return list;
    	}
    	public void setList(List<ContactsMakeFriendEntity> list) {
    		this.list = list;
    	}
    }
    public class ContactsMakeFriendEntity {


    	private String contact_name="";
    	private String contact_number="";
    	private String contact_photo_url="";
    	public String getContact_name() {
    		return contact_name;
    	}
    	public void setContact_name(String contact_name) {
    		this.contact_name = contact_name;
    	}

    	public String getContact_photo_url() {
    		return contact_photo_url;
    	}
    	public void setContact_photo_url(String contact_photo_url) {
    		this.contact_photo_url = contact_photo_url;
    	}
    	public String getContact_number() {
    		return contact_number;
    	}
    	public void setContact_number(String contact_number) {
    		this.contact_number = contact_number;
    	}

    }
    public class ContactsMakeFriendFailureResponse {
    	private  int code=-302;
    	private String msg="makefriend failure";
    	public int getCode() {
    		return code;
    	}
    	public void setCode(int code) {
    		this.code = code;
    	}
    	public String getMsg() {
    		return msg;
    	}
    	public void setMsg(String msg) {
    		this.msg = msg;
    	}
    }
}
